/*
    YmsgIRC - Program enabling YMSG chat clients to use IRC
    Copyright (C) 2011 Crash Daemonicus ( crashenator -at- gmail.com )

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "YMSG_head.h"

YmsgHead::YmsgHead(){ 
    dbg_head("YmsgHead()");
    //dbg_head("YmsgHead()");
    reset();
    dbg_foot(0);
}
void YmsgHead::reset(){  availDat=false;availRef=false;}
//void YmsgHead::setDat(tYmsgHead* pt){  memcpy(&Dat,pt,sizeof(tYmsgHead));availDat=true ;availRef=false;}




void YmsgHead::setDat(const tYmsgHead& t){  Dat=t; availDat=true ;availRef=false;}
void YmsgHead::setRef(const tYmsgHead& t){  Ref=t; availDat=false;availRef=true ;}
void YmsgHead::setDat(const tYmsgHead* t){ dbg_head("YmsgHead::setDat(tYmsgHead* t)"); Dat=*t; availDat=true ;availRef=false;dbg_foot(0);}

void YmsgHead::translate(tYmsgHead& t){
    //dbg_head("YmsgHead::translate(tYmsgHead& t)"); 
    t.version=RWORD(t.version);
    t.vendorID=RWORD(t.vendorID);
    t.contentLength=RWORD(t.contentLength);
    t.type=RWORD(t.type);
    MemReverse(&t.status,sizeof(t.status));
    MemReverse(&t.sessionID,sizeof(t.sessionID));
    //dbg_foot(0);
}
void YmsgHead::translate(){
    //dbg_head("YmsgHead::translate()"); 
    if(availDat && !availRef){
        Ref=Dat;translate(Ref);availRef=true;
    }else if(!availDat && availRef){
        Dat=Ref;translate(Dat);availDat=true;
    }
    //dbg_foot(0);
}

void YmsgHead::set(int typ,int len,int sta,int sid,int vid,int ver)
{
	Ref.type=typ;
    Ref.status=sta;
    Ref.contentLength=len;
    Ref.sessionID=sid;
    availRef=true;
    availDat=false;
    translate();
}

void YmsgHead::print(){
    //dbg_head("YmsgHead::print()");
    printf("\nHeader size %d bytes.\n\n",sizeof(tYmsgHead));
    //dbgline("cond1\n");
    if(availRef){
        //dbgline("alloc\n");
        char disp[10];
        //dbgline("memcpy\n");
        memcpy(disp,&Ref.signature,4); disp[4]=0; cputs(disp);
        //dbgline("itoa print\n");
        itoa(Ref.version,disp,10);cputs(disp);
        cputs(" vid:");
        itoa(Ref.vendorID,disp,10);cputs(disp);
        cputs(" len:");
        itoa(Ref.contentLength,disp,10);cputs(disp);
        cputs(" typ:");
        itoa(Ref.type,disp,16);cputs(disp);//******
        cputs(" sta:");
        itoa(Ref.status,disp,10);cputs(disp);
        cputs(" sid:");
        itoa(Ref.sessionID,disp,16);cputs(disp);//******
        cputs("\n");
    }
    //dbgline("cond2\n");
    if(availDat){
        //dbg_head(0); 
        //dbgline("looped printf\n");
        for(int i=0;i<20;i++){
            printf("%.2x", *((unsigned char*)(&Dat) + i) );
            switch(i){
                case 3:
                case 5:
                case 7:
                case 9:
                case 11:
                case 15:
                    putchar(0x20);
                    //puts(" ");
            }
        }puts("");
        //dbg_foot(0);
    }
}
void YmsgHead::copySession(const YmsgHead& head)
{
	Dat.sessionID=head.Dat.sessionID;
	Dat.version=head.Dat.version;
	Dat.vendorID=head.Dat.vendorID;

	Ref.sessionID=head.Ref.sessionID;
	Ref.version=head.Ref.version;
	Ref.vendorID=head.Ref.vendorID;
}